#include "GamestateManager.h"
#include "GameData.h"
#include "GameState.h"
#ifdef DEBUG_NEW
#include "debug/debug_new.h"
#endif

GamestateManager::GamestateManager() {
}

GamestateManager::~GamestateManager() {
    for (unordered_map<string, Gamestate*>::iterator it = _states.begin();
            it != _states.end(); ++it) {
        delete it->second;
    }
}

void GamestateManager::add_state(string name, Gamestate* state) {
    _states[name] = state;
}

void GamestateManager::push_state(string state) {
    if (_state_list.size() > 0) {
        _state_list.back()->pause();
    }
    _states.at(state)->begin();
    _state_list.push_back(_states.at(state));
}

void GamestateManager::pop_state() {
    if (_state_list.empty()) return;
    _state_list.back()->die();
    _state_list.pop_back();
    _state_list.back()->resume();
}

Gamestate* GamestateManager::current_state() {
    return _state_list.back();
}

void GamestateManager::input(SDL_Event* evt, unsigned int ms) {
    _state_list.back()->input(evt, ms);
}

void GamestateManager::update(unsigned int ms) {
    _state_list.back()->update(ms);
}

void GamestateManager::draw() {
    _state_list.back()->draw();
}